package com.azt.provider.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azt.api.pojo.Area;
import com.azt.api.service.AreaService;
import com.azt.provider.mapper.AreaMapper;
import com.xiaoleilu.hutool.util.StrUtil;

import tk.mybatis.mapper.entity.Example;


@Service
@Transactional
public class AreaServiceImpl  implements AreaService{

    /**
     * 存放中国省份 key:省份code val:省份名称
     */
    //private static Map<String, String> PROVINCES = new HashMap<String, String>();
    
    @Autowired
    private AreaMapper am;

    @Override
    public List<Area> findAllArea() {
        return am.selectAll();
    }
    
    /**
     * 重载方法
     * 
     * @param i
     * @return
     */
    //@Cacheable(value = "area", key = "'area.id.' + #pid")
    @Override
    public List<Area> getAreaByParentId(Integer pid)
    {
        return pid == null ? null : am.getAreaByParnetId(pid);
    }
    
    /**
     * 重载方法
     * 
     * @param id
     * @return
     */
    //@Cacheable(value = "area", key = "'area.id.' + #id")
    @Override
    public Area getAreaById(Integer id)
    {
        return am.selectByPrimaryKey(id);
    }
    
    /**
     * 重载方法
     * 
     * @param id
     */
    //@CacheEvict(value = "area", key = "'area.id.' + #id")
    @Override
    public void deleteAllById(Integer id)
    {
        am.deleteByPrimaryKey(id);
    }
    
    /**
     * 重载方法
     * 
     * @param code
     * @return
     */
    //@Cacheable(value = "area", key = "'area:code:' + #code")
    @Override
    public Area getAreaByCode(String code) {
        if(StrUtil.isBlank(code)) {
            return null;
        }
        Area area = new Area();
        area.setCode(code);
        return am.selectOne(area);
    }
    
    @Override
    public List<Area> getAreaByParentCode(String code) {
    	return code == null ? null : am.getAreaByParnetCode(code);
    }
    
    @Override
    public void saveOrUpdate(Area old)
    {
        if(old.getId() != null){
        	am.updateByPrimaryKey(old);
        }else{
        	am.insert(old);
        }
    }

    /*@Caching(
            put = {
                    @CachePut(value = "area", key = "'area.id.' + #area.id"),
                    @CachePut(value = "area", key = "'area.code.' + #area.code")
            }
    )*/
    @Override
    public void save(Area area)
    {
        am.insert(area);
    }
    
//    @Override
//    public Map<String, String> getChinaProvinces()
//    {
//        
//        synchronized (PROVINCES)
//        {
//            if (PROVINCES.isEmpty())
//            {
//                Area china = cs.getUniqueByParams(Area.class, "code", "86");
//                Integer chinaid = china.getId();
//                List<Area> provinces = cs.getListByParams(Area.class, "parent.id", chinaid);
//                for (int i = 0; i < provinces.size(); i++)
//                {
//                    Area province = provinces.get(i);
//                    PROVINCES.put("86-" + province.getCode(), province.getName());
//                }
//            }
//        }
//        return PROVINCES;
//    }
    
    @Override
    public String findByIds(String ids)
    {
        return am.findNamesByIds(ids);
    }
    
    @Override
    public String findByCodes(String codes)
    {
    	return am.findNamesByCodes(codes);
    }
    
    @Override
    public String finfByCodesAndPid(String codes, String pid)
    {
        return am.findNamesByCodesAndPid(codes, Integer.parseInt(pid));
    }
    
    @Override
    public boolean findOtherArea(Area area)
    {
        List<Area> areas = null;
        Integer id = area.getId();
        String code = area.getCode();
        Integer parentid = area.getParentId();
        if (id != null && !"".equals(id))
        {
            areas = am.getByCodeNotId(code, id, parentid);
        }
        else
        {
            Example exp = new Example(Area.class);
            exp.createCriteria().andEqualTo("code", code).andEqualTo("parentid",parentid);
            areas = am.selectByExample(exp);
            
        }
        if (areas.size() > 0)
        {
            return true;
        }
        
        return false;
    }

	@Override
	public List<Area> findChinaAreas() {
		return am.getAreaByParnetCode("000000");
	}

    //@Cacheable(value = "area", key = "'area.code.' + #code")
	@Override
	public Area getAreaBycode(String code) {
		return am.getAreaByCode(code);
	}
}

